package cn.itcast.demo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.List;
import java.util.Set;

/**
 * @author mrchen
 * @date 2022/6/27 21:53
 */
@SpringBootTest
public class RedisDemo {
    @Autowired
    StringRedisTemplate redisTemplate;
    @Test
    public void keys(){
        // 开启redisTemplate 对事务的支持
        redisTemplate.setEnableTransactionSupport(true);
        // 开启事务
        redisTemplate.multi();
        try {
            redisTemplate.opsForValue().set("name","xiaoming");
            String name = redisTemplate.opsForValue().get("name");
            redisTemplate.opsForValue().set("name","xiao7");

            System.out.println(1/0);
            // 提交事务
            redisTemplate.exec();
        } catch (Exception e) {
            e.printStackTrace();
            // 取消事务
            redisTemplate.discard();
        }
    }
}
